<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%activity}}".
 *
 * @property int $id
 * @property string|null $name 名称
 * @property int|null $c_id 分类id
 * @property float|null $price 单价
 * @property string|null $start_time 活动开始时间
 * @property string|null $end_time 活动结束时间
 * @property int|null $total 总数
 * @property int|null $surplus 剩余数
 * @property string|null $details 详情
 * @property string|null $main_img 主图
 * @property string|null $img 图片
 * @property int|null $views 浏览量
 * @property int|null $likes_num 点赞数
 * @property string|null $create_time 创建时间
 * @property string|null $update_time 修改时间
 * @property string|null $province 省
 * @property string|null $city 市
 * @property string|null $county 县
 * @property string|null $address 地址
 * @property int|null $sort 排序
 * @property string|null $mobile 电话
 * @property int|null $status 状态
 * @property int|null $delete 删除
 * @property int|null $is_recommend 推荐
 * @property string|null $recommend_img 推荐位图片
 * @property string|null $long 经度
 * @property string|null $lat 纬度
 */
class Activity extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%activity}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['c_id', 'total', 'surplus', 'views', 'likes_num', 'sort', 'status', 'delete', 'is_recommend'], 'integer'],
            [['price'], 'number'],
            [['start_time', 'end_time', 'create_time', 'update_time'], 'safe'],
            [['details'], 'string'],
            [['name', 'main_img', 'img', 'province', 'city', 'county', 'address', 'mobile', 'recommend_img', 'long', 'lat'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'c_id' => '分类id',
            'price' => '单价',
            'start_time' => '活动开始时间',
            'end_time' => '活动结束时间',
            'total' => '总数',
            'surplus' => '剩余数',
            'details' => '详情',
            'main_img' => '主图',
            'img' => '图片',
            'views' => '浏览量',
            'likes_num' => '点赞数',
            'create_time' => '创建时间',
            'update_time' => '修改时间',
            'province' => '省',
            'city' => '市',
            'county' => '县',
            'address' => '地址',
            'sort' => '排序',
            'mobile' => '电话',
            'status' => '状态',
            'delete' => '删除',
            'is_recommend' => '推荐',
            'recommend_img' => '推荐位图片',
            'long' => '经度',
            'lat' => '纬度',
        ];
    }

    public function isEnter($userid,$act_id,$start_time,$end_time,$status11,$delete,$surplus)
    {
        $status_title = '报名';
        $status = 1;
        if ($userid){
            $exists = ActivityEnter::find()->where(['userid'=>$userid,'act_id'=>$act_id,'status'=>1])->exists();
            if ($exists){
                $status_title = '已报名';
                $status = 0;
            }
        }
        $time = time();
        if ($status){
            if (strtotime($start_time)>$time){
                $status_title = '未开始';
                $status = 0;
            }
        }
        if ($status){
            if ($time>strtotime($end_time)){
                $status_title = '已结束';
                $status = 0;
            }
        }
        if ($status){
            if ($surplus<=0){
                $status_title = '已结束';
                $status = 0;
            }
        }
        if ($status){
            if ($status11!==1){
                $status_title = '已关闭';
                $status = 0;
            }
        }
        if ($status){
            if ($delete!==0){
                $status_title = '已删除';
                $status = 0;
            }
        }
        return ['status'=>$status,'status_title'=>$status_title];
    }
}
